from variable import Variable
from operate import (
    confirm_step,
    left_click,
    pic_click,
    key_down,
    key_up,
    press,
)
import time
from threading import Thread, Lock

fight_quit = True
fight_direction = 0
lock = Lock()


def fighting(quitSrc=""):
    global fight_quit
    global fight_direction
    while not confirm_step(
        "images/fight/pause.png",
        confidence=0.6,
        w=100,
        h=70,
        bl=1550,
        bt=90,
    ):
        print("\rfighting加载中", end="")
        time.sleep(0.5)
    print("\n开始战斗")
    t0 = time.time()
    key_down("w")
    change_role = "c"
    fight_quit = False
    t1 = Thread(target=fighting_confirm_go)
    t1.start()
    t2 = Thread(target=fighting_confirm_quit, args=(quitSrc,))
    t2.start()
    tmp = fight_direction
    while True:
        if fight_quit:
            break
        for i in range(4):
            keyToxy("j")

        keyToxy("r")
        keyToxy("q")
        keyToxy("v")
        keyToxy("k")

        if tmp != fight_direction:
            press("right")
            tmp = fight_direction

        if (time.time() - t0) > 3 and not fight_quit:
            key_up("w")
            time.sleep(0.1)
            key_down("w")
            keyToxy(change_role)
            change_role = "x" if change_role == "c" else "c"
            t0 = time.time()
    key_up("w")
    t1.join()
    t2.join()
    print("战斗完成")


def fighting_confirm_quit(quitSrc):
    global fight_quit
    while True:
        flag = confirm_step(
            "images/fight/pause.png",
            confidence=0.8,
            w=100,
            h=70,
            bl=1550,
            bt=90,
            imgsrc="images/current1.png",
        )
        if not flag:
            time.sleep(1)
            if quitSrc == "":
                f = confirm_step(
                    "images/fight/pause.png",
                    confidence=0.8,
                    w=100,
                    h=70,
                    bl=1550,
                    bt=90,
                    imgsrc="images/current1.png",
                )
                if f:
                    continue
                else:
                    fight_quit = True
                    break
            if confirm_step(quitSrc):
                fight_quit = True
                time.sleep(1)
                while not pic_click(
                    src=quitSrc,
                    delay=1,
                    confirm=quitSrc,
                    conf_flag=False,
                ):
                    time.sleep(0.5)
                break
        else:
            time.sleep(2)


def fighting_confirm_go():
    global fight_quit, fight_direction
    to = time.time()
    number = 1
    while True:
        if fight_quit:
            break
        flag = confirm_step(
            "images/fight/combo.png",
            0.7,
            w=110,
            h=40,
            bl=487,
            bt=505,
            imgsrc="images/current0.png",
        )
        if not flag:
            if time.time() - to > 5:
                print(f"\r调整方向 X{number}", end="")
                number += 1
                fight_direction += 1
                to = time.time()
        else:
            to = time.time()
        time.sleep(1)


def keyToxy(key):
    tmp = []
    if key == "j":
        tmp = [Variable.width / 1.12, Variable.height / 1.22]
    if key == "k":
        tmp = [Variable.width / 1.276, Variable.height / 1.22]
    if key == "r":
        tmp = [Variable.width / 1.45, Variable.height / 1.22]
    if key == "q":
        tmp = [Variable.width / 1.2443, Variable.height / 1.377]
    if key == "c":
        tmp = [Variable.width / 1.11, Variable.height / 3.48]
    if key == "x":
        tmp = [Variable.width / 1.11, Variable.height / 2.38]
    if key == "v":
        tmp = [Variable.width / 1.11, Variable.height / 1.797]
    left_click(tmp[0], tmp[1], delay=0.1)
